<template>
  <a-row align="center" id="globalHeader" :wrap="false">
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        :selected-keys="selectedKeys"
        @menu-item-click="doMenuClick"
      >
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginRight: '38px' }"
          disabled
        >
          <div class="title-bar">
            <img src="@/assets/oj-logo.png" alt="" class="logo" />
            <div class="title">OJ网</div>
          </div>
        </a-menu-item>
        <a-menu-item v-for="item in visibleRoutes" :key="item.path">
          {{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>
    <a-col flex="160px">
      <a-space
        size="medium"
        align="center"
        style="justify-content: flex-end; width: 100%"
      >
        <div class="user-name">
          {{ isLogin ? store.state.user?.loginUser?.userName : "未登录" }}
        </div>
        <a-dropdown trigger="click">
          <a-avatar :size="36" style="cursor: pointer" @click="onAvatarClick">
            {{ avatarText }}
          </a-avatar>
          <template #content>
            <a-doption v-if="!isLogin" @click="goLogin">登录</a-doption>
            <a-doption v-else @click="handleLogout">退出登录</a-doption>
          </template>
        </a-dropdown>
      </a-space>
    </a-col>
  </a-row>
</template>

<script lang="ts" setup>
import { routes } from "../router/routes";
import { useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import ACCESS_ENUM from "@/access/accessEnum";
import { Message } from "@arco-design/web-vue";
import { UserControllerService } from "../../generated";

const router = useRouter();
const store = useStore();
//展示在菜单的路由数组
const visibleRoutes = computed(() => {
  return routes.filter((item, index) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    //根据权限过滤菜单
    if (!checkAccess(store.state.user.loginUser, item.meta?.access as string)) {
      return false;
    }
    return true;
  });
});
// const visibleRoutes = routes.filter((item, index) => {
//   if (item.meta?.hideInMenu) {
//     return false;
//   }
//   //根据权限过滤菜单
//   if (!checkAccess(store.state.user.loginUser, item.meta?.access as string)) {
//     return false;
//   }
//   return true;
// });

//默认主页
const selectedKeys = ref(["/"]);
//路由跳转后，更新激活项
router.afterEach((to, from, next) => {
  selectedKeys.value = [to.path];
});
const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};
const isLogin = computed(() => {
  const role = store.state.user?.loginUser?.userRole;
  return !!role && role !== ACCESS_ENUM.NOT_LOGIN;
});

const avatarText = computed(() => {
  const name = store.state.user?.loginUser?.userName as string | undefined;
  if (isLogin.value && name) {
    return name.substring(0, 1).toUpperCase();
  }
  return "登录";
});

const goLogin = () => {
  router.push({ path: "/user/login" });
};

const onAvatarClick = () => {
  if (!isLogin.value) {
    goLogin();
  }
};

const handleLogout = async () => {
  try {
    const res = await UserControllerService.userLogoutUsingPost();
    if (res?.code === 0 || res?.data === true) {
      Message.success("已退出登录");
    }
  } catch (e) {
    // ignore, still refresh user state
  } finally {
    await store.dispatch("user/getLoginUser");
    router.replace("/");
  }
};
// console.log(store.state.user);
// setTimeout(() => {
//   store.dispatch("user/getLoginUser", {
//     userName: "管理员",
//     userRole: ACCESS_ENUM.ADMIN,
//   });
// }, 3000);
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: #444;
  margin-left: 16px;
}

.logo {
  height: 48px;
}

.user-name {
  color: #444;
}
</style>
